package com.yunshang.shoppingmall.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yunshang.shoppingmall.common.HttpRequest;
import com.yunshang.shoppingmall.entity.Logistics;
import com.yunshang.shoppingmall.entity.logtype;
import com.yunshang.shoppingmall.mapper.LogisticsMapper;
import com.yunshang.shoppingmall.service.LogisticsService;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import static com.alibaba.fastjson.JSON.parseObject;

@Service("LogisticsService")
public class LogisticsServiceImpl implements LogisticsService {

    @Autowired
    LogisticsMapper logisticsMapper;
    @Override
    public int deleteByPrimaryKey(Integer logisticsId) {
        return 0;
    }

    @Override
    public int insert(Logistics record) {
        return logisticsMapper.insert(record);
    }

    @Override
    public int insertSelective(Logistics record) {
        return 0;
    }

    @Override
    public Logistics selectByPrimaryKey(Integer logisticsId) {
        return logisticsMapper.selectByPrimaryKey(logisticsId);
    }

    @Override
    public int updateByPrimaryKeySelective(Logistics record) {
        return logisticsMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(Logistics record) {
        return logisticsMapper.updateByPrimaryKeyWithBLOBs(record);
    }

    @Override
    public int updateByPrimaryKey(Logistics record) {
        return  logisticsMapper.updateByPrimaryKey(record);
    }

    @Override
    public Logistics selectByNo(String logisticsNo) {
        return  logisticsMapper.selectByNo(logisticsNo);
    }
private void getlkl(){

}
    @Override
    public String getLogType(String logno) {
        String expressKey = "nnzeDXbl6758";
        try {
            if (logno!=null) {
//                logno="822492462394";
                String url = "http://www.kuaidi100.com/autonumber/auto";
                String userNoId = "num=" + logno;
                userNoId +="&key"+expressKey;
                String s="";
                String result = HttpRequest.sendGet(url, userNoId);

                List<logtype> datas = null;
                JSONArray jsonArr = JSONArray.fromObject(result);
                datas = (List<logtype>) JSONArray.toCollection(jsonArr, logtype.class);
                //JSONObject str = JSONObject.parseObject(result);
                if(datas!=null&&datas.size()>0){
                    logtype logtype1=datas.get(0);
                    String type=logtype1.getComCode();
                    //System.out.println("a1");
                    return type;
                }else {
                    //System.out.println("a2");
                    return "huitongkuaidi";
                }
            }else {
                //System.out.println("a3");
                return "huitongkuaidi";
            }
        }catch (Exception e){
            //System.out.println("a4");
            return "huitongkuaidi";
        }
    }
}
